<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>16.2. io — Core tools for working with streams &mdash; Python v2.6.2 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.6.2',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v2.6.2 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v2.6.2 documentation" href="../index.html" />
    <link rel="up" title="16. Generic Operating System Services" href="allos.html" />
    <link rel="next" title="16.3. time — Time access and conversions" href="time.html" />
    <link rel="prev" title="16.1. os — Miscellaneous operating system interfaces" href="os.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="time.html" title="16.3. time — Time access and conversions"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="os.html" title="16.1. os — Miscellaneous operating system interfaces"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="allos.html" accesskey="U">16. Generic Operating System Services</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-io">
<h1>16.2. <tt class="xref docutils literal"><span class="pre">io</span></tt> &#8212; Core tools for working with streams<a class="headerlink" href="#module-io" title="Permalink to this headline">¶</a></h1>
<p>
<span class="versionmodified">New in version 2.6.</span></p>
<p>The <tt class="xref docutils literal"><span class="pre">io</span></tt> module provides the Python interfaces to stream handling.  The
builtin <a title="open" class="reference external" href="functions.html#open"><tt class="xref docutils literal"><span class="pre">open()</span></tt></a> function is defined in this module.</p>
<p>At the top of the I/O hierarchy is the abstract base class <a title="io.IOBase" class="reference internal" href="#io.IOBase"><tt class="xref docutils literal"><span class="pre">IOBase</span></tt></a>.  It
defines the basic interface to a stream.  Note, however, that there is no
seperation between reading and writing to streams; implementations are allowed
to throw an <a title="exceptions.IOError" class="reference external" href="exceptions.html#exceptions.IOError"><tt class="xref docutils literal"><span class="pre">IOError</span></tt></a> if they do not support a given operation.</p>
<p>Extending <a title="io.IOBase" class="reference internal" href="#io.IOBase"><tt class="xref docutils literal"><span class="pre">IOBase</span></tt></a> is <a title="io.RawIOBase" class="reference internal" href="#io.RawIOBase"><tt class="xref docutils literal"><span class="pre">RawIOBase</span></tt></a> which deals simply with the
reading and writing of raw bytes to a stream.  <a title="io.FileIO" class="reference internal" href="#io.FileIO"><tt class="xref docutils literal"><span class="pre">FileIO</span></tt></a> subclasses
<a title="io.RawIOBase" class="reference internal" href="#io.RawIOBase"><tt class="xref docutils literal"><span class="pre">RawIOBase</span></tt></a> to provide an interface to files in the machine&#8217;s
file system.</p>
<p><a title="io.BufferedIOBase" class="reference internal" href="#io.BufferedIOBase"><tt class="xref docutils literal"><span class="pre">BufferedIOBase</span></tt></a> deals with buffering on a raw byte stream
(<a title="io.RawIOBase" class="reference internal" href="#io.RawIOBase"><tt class="xref docutils literal"><span class="pre">RawIOBase</span></tt></a>).  Its subclasses, <a title="io.BufferedWriter" class="reference internal" href="#io.BufferedWriter"><tt class="xref docutils literal"><span class="pre">BufferedWriter</span></tt></a>,
<a title="io.BufferedReader" class="reference internal" href="#io.BufferedReader"><tt class="xref docutils literal"><span class="pre">BufferedReader</span></tt></a>, and <a title="io.BufferedRWPair" class="reference internal" href="#io.BufferedRWPair"><tt class="xref docutils literal"><span class="pre">BufferedRWPair</span></tt></a> buffer streams that are
readable, writable, and both readable and writable.
<a title="io.BufferedRandom" class="reference internal" href="#io.BufferedRandom"><tt class="xref docutils literal"><span class="pre">BufferedRandom</span></tt></a> provides a buffered interface to random access
streams.  <a title="io.BytesIO" class="reference internal" href="#io.BytesIO"><tt class="xref docutils literal"><span class="pre">BytesIO</span></tt></a> is a simple stream of in-memory bytes.</p>
<p>Another <a title="io.IOBase" class="reference internal" href="#io.IOBase"><tt class="xref docutils literal"><span class="pre">IOBase</span></tt></a> subclass, <a title="io.TextIOBase" class="reference internal" href="#io.TextIOBase"><tt class="xref docutils literal"><span class="pre">TextIOBase</span></tt></a>, deals with
streams whose bytes represent text, and handles encoding and decoding
from and to strings. <a title="io.TextIOWrapper" class="reference internal" href="#io.TextIOWrapper"><tt class="xref docutils literal"><span class="pre">TextIOWrapper</span></tt></a>, which extends it, is a
buffered text interface to a buffered raw stream
(<a title="io.BufferedIOBase" class="reference internal" href="#io.BufferedIOBase"><tt class="xref docutils literal"><span class="pre">BufferedIOBase</span></tt></a>). Finally, <a title="io.StringIO" class="reference internal" href="#io.StringIO"><tt class="xref docutils literal"><span class="pre">StringIO</span></tt></a> is an in-memory
stream for text.</p>
<p>Argument names are not part of the specification, and only the arguments of
<a title="open" class="reference external" href="functions.html#open"><tt class="xref docutils literal"><span class="pre">open()</span></tt></a> are intended to be used as keyword arguments.</p>
<div class="section" id="module-interface">
<h2>16.2.1. Module Interface<a class="headerlink" href="#module-interface" title="Permalink to this headline">¶</a></h2>
<dl class="data">
<dt id="io.DEFAULT_BUFFER_SIZE">
<tt class="descclassname">io.</tt><tt class="descname">DEFAULT_BUFFER_SIZE</tt><a class="headerlink" href="#io.DEFAULT_BUFFER_SIZE" title="Permalink to this definition">¶</a></dt>
<dd>An int containing the default buffer size used by the module&#8217;s buffered I/O
classes.  <a title="open" class="reference external" href="functions.html#open"><tt class="xref docutils literal"><span class="pre">open()</span></tt></a> uses the file&#8217;s blksize (as obtained by
<a title="os.stat" class="reference external" href="os.html#os.stat"><tt class="xref docutils literal"><span class="pre">os.stat()</span></tt></a>) if possible.</dd></dl>

<dl class="function">
<dt id="io.open">
<tt class="descclassname">io.</tt><tt class="descname">open</tt><big>(</big><em>file</em><span class="optional">[</span>, <em>mode</em><span class="optional">[</span>, <em>buffering</em><span class="optional">[</span>, <em>encoding</em><span class="optional">[</span>, <em>errors</em><span class="optional">[</span>, <em>newline</em><span class="optional">[</span>, <em>closefd=True</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#io.open" title="Permalink to this definition">¶</a></dt>
<dd><p>Open <em>file</em> and return a stream.  If the file cannot be opened, an
<a title="exceptions.IOError" class="reference external" href="exceptions.html#exceptions.IOError"><tt class="xref docutils literal"><span class="pre">IOError</span></tt></a> is raised.</p>
<p><em>file</em> is either a string giving the name (and the path if the file isn&#8217;t in
the current working directory) of the file to be opened or a file
descriptor of the file to be opened.  (If a file descriptor is given,
for example, from <a title="os.fdopen" class="reference external" href="os.html#os.fdopen"><tt class="xref docutils literal"><span class="pre">os.fdopen()</span></tt></a>, it is closed when the returned
I/O object is closed, unless <em>closefd</em> is set to <tt class="xref docutils literal"><span class="pre">False</span></tt>.)</p>
<p><em>mode</em> is an optional string that specifies the mode in which the file is
opened.  It defaults to <tt class="docutils literal"><span class="pre">'r'</span></tt> which means open for reading in text mode.
Other common values are <tt class="docutils literal"><span class="pre">'w'</span></tt> for writing (truncating the file if it
already exists), and <tt class="docutils literal"><span class="pre">'a'</span></tt> for appending (which on <em>some</em> Unix systems,
means that <em>all</em> writes append to the end of the file regardless of the
current seek position).  In text mode, if <em>encoding</em> is not specified the
encoding used is platform dependent. (For reading and writing raw bytes use
binary mode and leave <em>encoding</em> unspecified.)  The available modes are:</p>
<table border="1" class="docutils">
<colgroup>
<col width="13%" />
<col width="88%" />
</colgroup>
<tbody valign="top">
<tr><td>Character</td>
<td>Meaning</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'r'</span></tt></td>
<td>open for reading (default)</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'w'</span></tt></td>
<td>open for writing, truncating the file first</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'a'</span></tt></td>
<td>open for writing, appending to the end of the file if it exists</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'b'</span></tt></td>
<td>binary mode</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'t'</span></tt></td>
<td>text mode (default)</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'+'</span></tt></td>
<td>open a disk file for updating (reading and writing)</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'U'</span></tt></td>
<td>universal newline mode (for backwards compatibility; should
not be used in new code)</td>
</tr>
</tbody>
</table>
<p>The default mode is <tt class="docutils literal"><span class="pre">'rt'</span></tt> (open for reading text).  For binary random
access, the mode <tt class="docutils literal"><span class="pre">'w+b'</span></tt> opens and truncates the file to 0 bytes, while
<tt class="docutils literal"><span class="pre">'r+b'</span></tt> opens the file without truncation.</p>
<p>Python distinguishes between files opened in binary and text modes, even when
the underlying operating system doesn&#8217;t.  Files opened in binary mode
(including <tt class="docutils literal"><span class="pre">'b'</span></tt> in the <em>mode</em> argument) return contents as <tt class="docutils literal"><span class="pre">bytes</span></tt>
objects without any decoding.  In text mode (the default, or when <tt class="docutils literal"><span class="pre">'t'</span></tt> is
included in the <em>mode</em> argument), the contents of the file are returned as
strings, the bytes having been first decoded using a platform-dependent
encoding or using the specified <em>encoding</em> if given.</p>
<p><em>buffering</em> is an optional integer used to set the buffering policy.  By
default full buffering is on.  Pass 0 to switch buffering off (only allowed
in binary mode), 1 to set line buffering, and an integer &gt; 1 for full
buffering.</p>
<p><em>encoding</em> is the name of the encoding used to decode or encode the file.
This should only be used in text mode.  The default encoding is platform
dependent, but any encoding supported by Python can be used.  See the
<a title="Encode and decode data and streams." class="reference external" href="codecs.html#module-codecs"><tt class="xref docutils literal"><span class="pre">codecs</span></tt></a> module for the list of supported encodings.</p>
<p><em>errors</em> is an optional string that specifies how encoding and decoding
errors are to be handled.  Pass <tt class="docutils literal"><span class="pre">'strict'</span></tt> to raise a <a title="exceptions.ValueError" class="reference external" href="exceptions.html#exceptions.ValueError"><tt class="xref docutils literal"><span class="pre">ValueError</span></tt></a>
exception if there is an encoding error (the default of <tt class="xref docutils literal"><span class="pre">None</span></tt> has the same
effect), or pass <tt class="docutils literal"><span class="pre">'ignore'</span></tt> to ignore errors.  (Note that ignoring encoding
errors can lead to data loss.)  <tt class="docutils literal"><span class="pre">'replace'</span></tt> causes a replacement marker
(such as <tt class="docutils literal"><span class="pre">'?'</span></tt>) to be inserted where there is malformed data.  When
writing, <tt class="docutils literal"><span class="pre">'xmlcharrefreplace'</span></tt> (replace with the appropriate XML character
reference) or <tt class="docutils literal"><span class="pre">'backslashreplace'</span></tt> (replace with backslashed escape
sequences) can be used.  Any other error handling name that has been
registered with <a title="codecs.register_error" class="reference external" href="codecs.html#codecs.register_error"><tt class="xref docutils literal"><span class="pre">codecs.register_error()</span></tt></a> is also valid.</p>
<p><em>newline</em> controls how universal newlines works (it only applies to text
mode).  It can be <tt class="xref docutils literal"><span class="pre">None</span></tt>, <tt class="docutils literal"><span class="pre">''</span></tt>, <tt class="docutils literal"><span class="pre">'\n'</span></tt>, <tt class="docutils literal"><span class="pre">'\r'</span></tt>, and <tt class="docutils literal"><span class="pre">'\r\n'</span></tt>.  It
works as follows:</p>
<ul class="simple">
<li>On input, if <em>newline</em> is <tt class="xref docutils literal"><span class="pre">None</span></tt>, universal newlines mode is enabled.
Lines in the input can end in <tt class="docutils literal"><span class="pre">'\n'</span></tt>, <tt class="docutils literal"><span class="pre">'\r'</span></tt>, or <tt class="docutils literal"><span class="pre">'\r\n'</span></tt>, and these
are translated into <tt class="docutils literal"><span class="pre">'\n'</span></tt> before being returned to the caller.  If it is
<tt class="docutils literal"><span class="pre">''</span></tt>, universal newline mode is enabled, but line endings are returned to
the caller untranslated.  If it has any of the other legal values, input
lines are only terminated by the given string, and the line ending is
returned to the caller untranslated.</li>
<li>On output, if <em>newline</em> is <tt class="xref docutils literal"><span class="pre">None</span></tt>, any <tt class="docutils literal"><span class="pre">'\n'</span></tt> characters written are
translated to the system default line separator, <a title="os.linesep" class="reference external" href="os.html#os.linesep"><tt class="xref docutils literal"><span class="pre">os.linesep</span></tt></a>.  If
<em>newline</em> is <tt class="docutils literal"><span class="pre">''</span></tt>, no translation takes place.  If <em>newline</em> is any of
the other legal values, any <tt class="docutils literal"><span class="pre">'\n'</span></tt> characters written are translated to
the given string.</li>
</ul>
<p>If <em>closefd</em> is <tt class="xref docutils literal"><span class="pre">False</span></tt> and a file descriptor rather than a
filename was given, the underlying file descriptor will be kept open
when the file is closed.  If a filename is given <em>closefd</em> has no
effect but must be <tt class="xref docutils literal"><span class="pre">True</span></tt> (the default).</p>
<p>The type of file object returned by the <a title="open" class="reference external" href="functions.html#open"><tt class="xref docutils literal"><span class="pre">open()</span></tt></a> function depends
on the mode.  When <a title="open" class="reference external" href="functions.html#open"><tt class="xref docutils literal"><span class="pre">open()</span></tt></a> is used to open a file in a text mode
(<tt class="docutils literal"><span class="pre">'w'</span></tt>, <tt class="docutils literal"><span class="pre">'r'</span></tt>, <tt class="docutils literal"><span class="pre">'wt'</span></tt>, <tt class="docutils literal"><span class="pre">'rt'</span></tt>, etc.), it returns a
<a title="io.TextIOWrapper" class="reference internal" href="#io.TextIOWrapper"><tt class="xref docutils literal"><span class="pre">TextIOWrapper</span></tt></a>. When used to open a file in a binary mode,
the returned class varies: in read binary mode, it returns a
<a title="io.BufferedReader" class="reference internal" href="#io.BufferedReader"><tt class="xref docutils literal"><span class="pre">BufferedReader</span></tt></a>; in write binary and append binary modes, it
returns a <a title="io.BufferedWriter" class="reference internal" href="#io.BufferedWriter"><tt class="xref docutils literal"><span class="pre">BufferedWriter</span></tt></a>, and in read/write mode, it returns
a <a title="io.BufferedRandom" class="reference internal" href="#io.BufferedRandom"><tt class="xref docutils literal"><span class="pre">BufferedRandom</span></tt></a>.</p>
<p>It is also possible to use a string or bytearray as a file for both reading
and writing.  For strings <a title="io.StringIO" class="reference internal" href="#io.StringIO"><tt class="xref docutils literal"><span class="pre">StringIO</span></tt></a> can be used like a file opened in
a text mode, and for bytearrays a <a title="io.BytesIO" class="reference internal" href="#io.BytesIO"><tt class="xref docutils literal"><span class="pre">BytesIO</span></tt></a> can be used like a
file opened in a binary mode.</p>
</dd></dl>

<dl class="exception">
<dt id="io.BlockingIOError">
<em class="property">
exception </em><tt class="descclassname">io.</tt><tt class="descname">BlockingIOError</tt><a class="headerlink" href="#io.BlockingIOError" title="Permalink to this definition">¶</a></dt>
<dd><p>Error raised when blocking would occur on a non-blocking stream.  It inherits
<a title="exceptions.IOError" class="reference external" href="exceptions.html#exceptions.IOError"><tt class="xref docutils literal"><span class="pre">IOError</span></tt></a>.</p>
<p>In addition to those of <a title="exceptions.IOError" class="reference external" href="exceptions.html#exceptions.IOError"><tt class="xref docutils literal"><span class="pre">IOError</span></tt></a>, <a title="io.BlockingIOError" class="reference internal" href="#io.BlockingIOError"><tt class="xref docutils literal"><span class="pre">BlockingIOError</span></tt></a> has one
attribute:</p>
<dl class="attribute">
<dt id="io.BlockingIOError.characters_written">
<tt class="descname">characters_written</tt><a class="headerlink" href="#io.BlockingIOError.characters_written" title="Permalink to this definition">¶</a></dt>
<dd>An integer containing the number of characters written to the stream
before it blocked.</dd></dl>

</dd></dl>

<dl class="exception">
<dt id="io.UnsupportedOperation">
<em class="property">
exception </em><tt class="descclassname">io.</tt><tt class="descname">UnsupportedOperation</tt><a class="headerlink" href="#io.UnsupportedOperation" title="Permalink to this definition">¶</a></dt>
<dd>An exception inheriting <a title="exceptions.IOError" class="reference external" href="exceptions.html#exceptions.IOError"><tt class="xref docutils literal"><span class="pre">IOError</span></tt></a> and <a title="exceptions.ValueError" class="reference external" href="exceptions.html#exceptions.ValueError"><tt class="xref docutils literal"><span class="pre">ValueError</span></tt></a> that is raised
when an unsupported operation is called on a stream.</dd></dl>

</div>
<div class="section" id="i-o-base-classes">
<h2>16.2.2. I/O Base Classes<a class="headerlink" href="#i-o-base-classes" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="io.IOBase">
<em class="property">
class </em><tt class="descclassname">io.</tt><tt class="descname">IOBase</tt><a class="headerlink" href="#io.IOBase" title="Permalink to this definition">¶</a></dt>
<dd><p>The abstract base class for all I/O classes, acting on streams of bytes.
There is no public constructor.</p>
<p>This class provides empty abstract implementations for many methods
that derived classes can override selectively; the default
implementations represent a file that cannot be read, written or
seeked.</p>
<p>Even though <a title="io.IOBase" class="reference internal" href="#io.IOBase"><tt class="xref docutils literal"><span class="pre">IOBase</span></tt></a> does not declare <tt class="xref docutils literal"><span class="pre">read()</span></tt>, <tt class="xref docutils literal"><span class="pre">readinto()</span></tt>,
or <tt class="xref docutils literal"><span class="pre">write()</span></tt> because their signatures will vary, implementations and
clients should consider those methods part of the interface.  Also,
implementations may raise a <a title="exceptions.IOError" class="reference external" href="exceptions.html#exceptions.IOError"><tt class="xref docutils literal"><span class="pre">IOError</span></tt></a> when operations they do not
support are called.</p>
<p>The basic type used for binary data read from or written to a file is
<tt class="xref docutils literal"><span class="pre">bytes</span></tt>.  <tt class="xref docutils literal"><span class="pre">bytearray</span></tt>s are accepted too, and in some cases
(such as <tt class="xref docutils literal"><span class="pre">readinto</span></tt>) required.  Text I/O classes work with
<a title="str" class="reference external" href="functions.html#str"><tt class="xref docutils literal"><span class="pre">str</span></tt></a> data.</p>
<p>Note that calling any method (even inquiries) on a closed stream is
undefined.  Implementations may raise <a title="exceptions.IOError" class="reference external" href="exceptions.html#exceptions.IOError"><tt class="xref docutils literal"><span class="pre">IOError</span></tt></a> in this case.</p>
<p>IOBase (and its subclasses) support the iterator protocol, meaning that an
<a title="io.IOBase" class="reference internal" href="#io.IOBase"><tt class="xref docutils literal"><span class="pre">IOBase</span></tt></a> object can be iterated over yielding the lines in a stream.</p>
<p>IOBase is also a context manager and therefore supports the
<a class="reference external" href="../reference/compound_stmts.html#with"><tt class="xref docutils literal"><span class="pre">with</span></tt></a> statement.  In this example, <em>file</em> is closed after the
<a class="reference external" href="../reference/compound_stmts.html#with"><tt class="xref docutils literal"><span class="pre">with</span></tt></a> statement&#8217;s suite is finished&#8212;even if an exception occurs:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s">&#39;spam.txt&#39;</span><span class="p">,</span> <span class="s">&#39;w&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="nb">file</span><span class="p">:</span>
    <span class="nb">file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&#39;Spam and eggs!&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p><a title="io.IOBase" class="reference internal" href="#io.IOBase"><tt class="xref docutils literal"><span class="pre">IOBase</span></tt></a> provides these data attributes and methods:</p>
<dl class="method">
<dt id="io.IOBase.close">
<tt class="descname">close</tt><big>(</big><big>)</big><a class="headerlink" href="#io.IOBase.close" title="Permalink to this definition">¶</a></dt>
<dd>Flush and close this stream. This method has no effect if the file is
already closed. Once the file is closed, any operation on the file
(e.g. reading or writing) will raise an <a title="exceptions.IOError" class="reference external" href="exceptions.html#exceptions.IOError"><tt class="xref docutils literal"><span class="pre">IOError</span></tt></a>. The internal
file descriptor isn&#8217;t closed if <em>closefd</em> was False.</dd></dl>

<dl class="attribute">
<dt id="io.IOBase.closed">
<tt class="descname">closed</tt><a class="headerlink" href="#io.IOBase.closed" title="Permalink to this definition">¶</a></dt>
<dd>True if the stream is closed.</dd></dl>

<dl class="method">
<dt id="io.IOBase.fileno">
<tt class="descname">fileno</tt><big>(</big><big>)</big><a class="headerlink" href="#io.IOBase.fileno" title="Permalink to this definition">¶</a></dt>
<dd>Return the underlying file descriptor (an integer) of the stream if it
exists.  An <a title="exceptions.IOError" class="reference external" href="exceptions.html#exceptions.IOError"><tt class="xref docutils literal"><span class="pre">IOError</span></tt></a> is raised if the IO object does not use a file
descriptor.</dd></dl>

<dl class="method">
<dt id="io.IOBase.flush">
<tt class="descname">flush</tt><big>(</big><big>)</big><a class="headerlink" href="#io.IOBase.flush" title="Permalink to this definition">¶</a></dt>
<dd>Flush the write buffers of the stream if applicable.  This does nothing
for read-only and non-blocking streams.</dd></dl>

<dl class="method">
<dt id="io.IOBase.isatty">
<tt class="descname">isatty</tt><big>(</big><big>)</big><a class="headerlink" href="#io.IOBase.isatty" title="Permalink to this definition">¶</a></dt>
<dd>Return <tt class="xref docutils literal"><span class="pre">True</span></tt> if the stream is interactive (i.e., connected to
a terminal/tty device).</dd></dl>

<dl class="method">
<dt id="io.IOBase.readable">
<tt class="descname">readable</tt><big>(</big><big>)</big><a class="headerlink" href="#io.IOBase.readable" title="Permalink to this definition">¶</a></dt>
<dd>Return <tt class="xref docutils literal"><span class="pre">True</span></tt> if the stream can be read from.  If False, <tt class="xref docutils literal"><span class="pre">read()</span></tt>
will raise <a title="exceptions.IOError" class="reference external" href="exceptions.html#exceptions.IOError"><tt class="xref docutils literal"><span class="pre">IOError</span></tt></a>.</dd></dl>

<dl class="method">
<dt id="io.IOBase.readline">
<tt class="descname">readline</tt><big>(</big><span class="optional">[</span><em>limit</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#io.IOBase.readline" title="Permalink to this definition">¶</a></dt>
<dd><p>Read and return one line from the stream.  If <em>limit</em> is specified, at
most <em>limit</em> bytes will be read.</p>
<p>The line terminator is always <tt class="docutils literal"><span class="pre">b'\n'</span></tt> for binary files; for text files,
the <em>newlines</em> argument to <a title="open" class="reference external" href="functions.html#open"><tt class="xref docutils literal"><span class="pre">open()</span></tt></a> can be used to select the line
terminator(s) recognized.</p>
</dd></dl>

<dl class="method">
<dt id="io.IOBase.readlines">
<tt class="descname">readlines</tt><big>(</big><span class="optional">[</span><em>hint</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#io.IOBase.readlines" title="Permalink to this definition">¶</a></dt>
<dd>Read and return a list of lines from the stream.  <em>hint</em> can be specified
to control the number of lines read: no more lines will be read if the
total size (in bytes/characters) of all lines so far exceeds <em>hint</em>.</dd></dl>

<dl class="method">
<dt id="io.IOBase.seek">
<tt class="descname">seek</tt><big>(</big><em>offset</em><span class="optional">[</span>, <em>whence</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#io.IOBase.seek" title="Permalink to this definition">¶</a></dt>
<dd><p>Change the stream position to the given byte <em>offset</em>.  <em>offset</em> is
interpreted relative to the position indicated by <em>whence</em>.  Values for
<em>whence</em> are:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">0</span></tt> &#8211; start of the stream (the default); <em>offset</em> should be zero or positive</li>
<li><tt class="docutils literal"><span class="pre">1</span></tt> &#8211; current stream position; <em>offset</em> may be negative</li>
<li><tt class="docutils literal"><span class="pre">2</span></tt> &#8211; end of the stream; <em>offset</em> is usually negative</li>
</ul>
<p>Return the new absolute position.</p>
</dd></dl>

<dl class="method">
<dt id="io.IOBase.seekable">
<tt class="descname">seekable</tt><big>(</big><big>)</big><a class="headerlink" href="#io.IOBase.seekable" title="Permalink to this definition">¶</a></dt>
<dd>Return <tt class="xref docutils literal"><span class="pre">True</span></tt> if the stream supports random access.  If <tt class="xref docutils literal"><span class="pre">False</span></tt>,
<a title="io.IOBase.seek" class="reference internal" href="#io.IOBase.seek"><tt class="xref docutils literal"><span class="pre">seek()</span></tt></a>, <a title="io.IOBase.tell" class="reference internal" href="#io.IOBase.tell"><tt class="xref docutils literal"><span class="pre">tell()</span></tt></a> and <a title="io.IOBase.truncate" class="reference internal" href="#io.IOBase.truncate"><tt class="xref docutils literal"><span class="pre">truncate()</span></tt></a> will raise <a title="exceptions.IOError" class="reference external" href="exceptions.html#exceptions.IOError"><tt class="xref docutils literal"><span class="pre">IOError</span></tt></a>.</dd></dl>

<dl class="method">
<dt id="io.IOBase.tell">
<tt class="descname">tell</tt><big>(</big><big>)</big><a class="headerlink" href="#io.IOBase.tell" title="Permalink to this definition">¶</a></dt>
<dd>Return the current stream position.</dd></dl>

<dl class="method">
<dt id="io.IOBase.truncate">
<tt class="descname">truncate</tt><big>(</big><span class="optional">[</span><em>size</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#io.IOBase.truncate" title="Permalink to this definition">¶</a></dt>
<dd>Truncate the file to at most <em>size</em> bytes.  <em>size</em> defaults to the current
file position, as returned by <a title="io.IOBase.tell" class="reference internal" href="#io.IOBase.tell"><tt class="xref docutils literal"><span class="pre">tell()</span></tt></a>.</dd></dl>

<dl class="method">
<dt id="io.IOBase.writable">
<tt class="descname">writable</tt><big>(</big><big>)</big><a class="headerlink" href="#io.IOBase.writable" title="Permalink to this definition">¶</a></dt>
<dd>Return <tt class="xref docutils literal"><span class="pre">True</span></tt> if the stream supports writing.  If <tt class="xref docutils literal"><span class="pre">False</span></tt>,
<tt class="xref docutils literal"><span class="pre">write()</span></tt> and <a title="io.IOBase.truncate" class="reference internal" href="#io.IOBase.truncate"><tt class="xref docutils literal"><span class="pre">truncate()</span></tt></a> will raise <a title="exceptions.IOError" class="reference external" href="exceptions.html#exceptions.IOError"><tt class="xref docutils literal"><span class="pre">IOError</span></tt></a>.</dd></dl>

<dl class="method">
<dt id="io.IOBase.writelines">
<tt class="descname">writelines</tt><big>(</big><em>lines</em><big>)</big><a class="headerlink" href="#io.IOBase.writelines" title="Permalink to this definition">¶</a></dt>
<dd>Write a list of lines to the stream.  Line separators are not added, so it
is usual for each of the lines provided to have a line separator at the
end.</dd></dl>

</dd></dl>

<dl class="class">
<dt id="io.RawIOBase">
<em class="property">
class </em><tt class="descclassname">io.</tt><tt class="descname">RawIOBase</tt><a class="headerlink" href="#io.RawIOBase" title="Permalink to this definition">¶</a></dt>
<dd><p>Base class for raw binary I/O.  It inherits <a title="io.IOBase" class="reference internal" href="#io.IOBase"><tt class="xref docutils literal"><span class="pre">IOBase</span></tt></a>.  There is no
public constructor.</p>
<p>In addition to the attributes and methods from <a title="io.IOBase" class="reference internal" href="#io.IOBase"><tt class="xref docutils literal"><span class="pre">IOBase</span></tt></a>,
RawIOBase provides the following methods:</p>
<dl class="method">
<dt id="io.RawIOBase.read">
<tt class="descname">read</tt><big>(</big><span class="optional">[</span><em>n</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#io.RawIOBase.read" title="Permalink to this definition">¶</a></dt>
<dd>Read and return all the bytes from the stream until EOF, or if <em>n</em> is
specified, up to <em>n</em> bytes.  Only one system call is ever made.  An empty
bytes object is returned on EOF; <tt class="xref docutils literal"><span class="pre">None</span></tt> is returned if the object is set
not to block and has no data to read.</dd></dl>

<dl class="method">
<dt id="io.RawIOBase.readall">
<tt class="descname">readall</tt><big>(</big><big>)</big><a class="headerlink" href="#io.RawIOBase.readall" title="Permalink to this definition">¶</a></dt>
<dd>Read and return all the bytes from the stream until EOF, using multiple
calls to the stream if necessary.</dd></dl>

<dl class="method">
<dt id="io.RawIOBase.readinto">
<tt class="descname">readinto</tt><big>(</big><em>b</em><big>)</big><a class="headerlink" href="#io.RawIOBase.readinto" title="Permalink to this definition">¶</a></dt>
<dd>Read up to len(b) bytes into bytearray <em>b</em> and return the number of bytes
read.</dd></dl>

<dl class="method">
<dt id="io.RawIOBase.write">
<tt class="descname">write</tt><big>(</big><em>b</em><big>)</big><a class="headerlink" href="#io.RawIOBase.write" title="Permalink to this definition">¶</a></dt>
<dd>Write the given bytes or bytearray object, <em>b</em>, to the underlying raw
stream and return the number of bytes written (This is never less than
<tt class="docutils literal"><span class="pre">len(b)</span></tt>, since if the write fails, an <a title="exceptions.IOError" class="reference external" href="exceptions.html#exceptions.IOError"><tt class="xref docutils literal"><span class="pre">IOError</span></tt></a> will be raised).</dd></dl>

</dd></dl>

</div>
<div class="section" id="raw-file-i-o">
<h2>16.2.3. Raw File I/O<a class="headerlink" href="#raw-file-i-o" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="io.FileIO">
<em class="property">
class </em><tt class="descclassname">io.</tt><tt class="descname">FileIO</tt><big>(</big><em>name</em><span class="optional">[</span>, <em>mode</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#io.FileIO" title="Permalink to this definition">¶</a></dt>
<dd><p><a title="io.FileIO" class="reference internal" href="#io.FileIO"><tt class="xref docutils literal"><span class="pre">FileIO</span></tt></a> represents a file containing bytes data.  It implements
the <a title="io.RawIOBase" class="reference internal" href="#io.RawIOBase"><tt class="xref docutils literal"><span class="pre">RawIOBase</span></tt></a> interface (and therefore the <a title="io.IOBase" class="reference internal" href="#io.IOBase"><tt class="xref docutils literal"><span class="pre">IOBase</span></tt></a>
interface, too).</p>
<p>The <em>mode</em> can be <tt class="docutils literal"><span class="pre">'r'</span></tt>, <tt class="docutils literal"><span class="pre">'w'</span></tt> or <tt class="docutils literal"><span class="pre">'a'</span></tt> for reading (default), writing,
or appending.  The file will be created if it doesn&#8217;t exist when opened for
writing or appending; it will be truncated when opened for writing.  Add a
<tt class="docutils literal"><span class="pre">'+'</span></tt> to the mode to allow simultaneous reading and writing.</p>
<p>In addition to the attributes and methods from <a title="io.IOBase" class="reference internal" href="#io.IOBase"><tt class="xref docutils literal"><span class="pre">IOBase</span></tt></a> and
<a title="io.RawIOBase" class="reference internal" href="#io.RawIOBase"><tt class="xref docutils literal"><span class="pre">RawIOBase</span></tt></a>, <a title="io.FileIO" class="reference internal" href="#io.FileIO"><tt class="xref docutils literal"><span class="pre">FileIO</span></tt></a> provides the following data
attributes and methods:</p>
<dl class="attribute">
<dt id="io.FileIO.mode">
<tt class="descname">mode</tt><a class="headerlink" href="#io.FileIO.mode" title="Permalink to this definition">¶</a></dt>
<dd>The mode as given in the constructor.</dd></dl>

<dl class="attribute">
<dt id="io.FileIO.name">
<tt class="descname">name</tt><a class="headerlink" href="#io.FileIO.name" title="Permalink to this definition">¶</a></dt>
<dd>The file name.  This is the file descriptor of the file when no name is
given in the constructor.</dd></dl>

<dl class="method">
<dt id="io.FileIO.read">
<tt class="descname">read</tt><big>(</big><span class="optional">[</span><em>n</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#io.FileIO.read" title="Permalink to this definition">¶</a></dt>
<dd>Read and return at most <em>n</em> bytes.  Only one system call is made, so it is
possible that less data than was requested is returned.  Use <a title="len" class="reference external" href="functions.html#len"><tt class="xref docutils literal"><span class="pre">len()</span></tt></a>
on the returned bytes object to see how many bytes were actually returned.
(In non-blocking mode, <tt class="xref docutils literal"><span class="pre">None</span></tt> is returned when no data is available.)</dd></dl>

<dl class="method">
<dt id="io.FileIO.readall">
<tt class="descname">readall</tt><big>(</big><big>)</big><a class="headerlink" href="#io.FileIO.readall" title="Permalink to this definition">¶</a></dt>
<dd>Read and return the entire file&#8217;s contents in a single bytes object.  As
much as immediately available is returned in non-blocking mode.  If the
EOF has been reached, <tt class="docutils literal"><span class="pre">b''</span></tt> is returned.</dd></dl>

<dl class="method">
<dt id="io.FileIO.write">
<tt class="descname">write</tt><big>(</big><em>b</em><big>)</big><a class="headerlink" href="#io.FileIO.write" title="Permalink to this definition">¶</a></dt>
<dd>Write the bytes or bytearray object, <em>b</em>, to the file, and return
the number actually written. Only one system call is made, so it
is possible that only some of the data is written.</dd></dl>

<p>Note that the inherited <tt class="docutils literal"><span class="pre">readinto()</span></tt> method should not be used on
<a title="io.FileIO" class="reference internal" href="#io.FileIO"><tt class="xref docutils literal"><span class="pre">FileIO</span></tt></a> objects.</p>
</dd></dl>

</div>
<div class="section" id="buffered-streams">
<h2>16.2.4. Buffered Streams<a class="headerlink" href="#buffered-streams" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="io.BufferedIOBase">
<em class="property">
class </em><tt class="descclassname">io.</tt><tt class="descname">BufferedIOBase</tt><a class="headerlink" href="#io.BufferedIOBase" title="Permalink to this definition">¶</a></dt>
<dd><p>Base class for streams that support buffering.  It inherits <a title="io.IOBase" class="reference internal" href="#io.IOBase"><tt class="xref docutils literal"><span class="pre">IOBase</span></tt></a>.
There is no public constructor.</p>
<p>The main difference with <a title="io.RawIOBase" class="reference internal" href="#io.RawIOBase"><tt class="xref docutils literal"><span class="pre">RawIOBase</span></tt></a> is that the <a title="io.BufferedIOBase.read" class="reference internal" href="#io.BufferedIOBase.read"><tt class="xref docutils literal"><span class="pre">read()</span></tt></a> method
supports omitting the <em>size</em> argument, and does not have a default
implementation that defers to <a title="io.BufferedIOBase.readinto" class="reference internal" href="#io.BufferedIOBase.readinto"><tt class="xref docutils literal"><span class="pre">readinto()</span></tt></a>.</p>
<p>In addition, <a title="io.BufferedIOBase.read" class="reference internal" href="#io.BufferedIOBase.read"><tt class="xref docutils literal"><span class="pre">read()</span></tt></a>, <a title="io.BufferedIOBase.readinto" class="reference internal" href="#io.BufferedIOBase.readinto"><tt class="xref docutils literal"><span class="pre">readinto()</span></tt></a>, and <a title="io.BufferedIOBase.write" class="reference internal" href="#io.BufferedIOBase.write"><tt class="xref docutils literal"><span class="pre">write()</span></tt></a> may raise
<a title="io.BlockingIOError" class="reference internal" href="#io.BlockingIOError"><tt class="xref docutils literal"><span class="pre">BlockingIOError</span></tt></a> if the underlying raw stream is in non-blocking mode
and not ready; unlike their raw counterparts, they will never return
<tt class="xref docutils literal"><span class="pre">None</span></tt>.</p>
<p>A typical implementation should not inherit from a <a title="io.RawIOBase" class="reference internal" href="#io.RawIOBase"><tt class="xref docutils literal"><span class="pre">RawIOBase</span></tt></a>
implementation, but wrap one like <a title="io.BufferedWriter" class="reference internal" href="#io.BufferedWriter"><tt class="xref docutils literal"><span class="pre">BufferedWriter</span></tt></a> and
<a title="io.BufferedReader" class="reference internal" href="#io.BufferedReader"><tt class="xref docutils literal"><span class="pre">BufferedReader</span></tt></a>.</p>
<p><a title="io.BufferedIOBase" class="reference internal" href="#io.BufferedIOBase"><tt class="xref docutils literal"><span class="pre">BufferedIOBase</span></tt></a> provides or overrides these methods in addition to
those from <a title="io.IOBase" class="reference internal" href="#io.IOBase"><tt class="xref docutils literal"><span class="pre">IOBase</span></tt></a>:</p>
<dl class="method">
<dt id="io.BufferedIOBase.read">
<tt class="descname">read</tt><big>(</big><span class="optional">[</span><em>n</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#io.BufferedIOBase.read" title="Permalink to this definition">¶</a></dt>
<dd><p>Read and return up to <em>n</em> bytes.  If the argument is omitted, <tt class="xref docutils literal"><span class="pre">None</span></tt>, or
negative, data is read and returned until EOF is reached.  An empty bytes
object is returned if the stream is already at EOF.</p>
<p>If the argument is positive, and the underlying raw stream is not
interactive, multiple raw reads may be issued to satisfy the byte count
(unless EOF is reached first).  But for interactive raw streams, at most
one raw read will be issued, and a short result does not imply that EOF is
imminent.</p>
<p>A <a title="io.BlockingIOError" class="reference internal" href="#io.BlockingIOError"><tt class="xref docutils literal"><span class="pre">BlockingIOError</span></tt></a> is raised if the underlying raw stream has no
data at the moment.</p>
</dd></dl>

<dl class="method">
<dt id="io.BufferedIOBase.readinto">
<tt class="descname">readinto</tt><big>(</big><em>b</em><big>)</big><a class="headerlink" href="#io.BufferedIOBase.readinto" title="Permalink to this definition">¶</a></dt>
<dd><p>Read up to len(b) bytes into bytearray <em>b</em> and return the number of bytes
read.</p>
<p>Like <a title="io.BufferedIOBase.read" class="reference internal" href="#io.BufferedIOBase.read"><tt class="xref docutils literal"><span class="pre">read()</span></tt></a>, multiple reads may be issued to the underlying raw
stream, unless the latter is &#8216;interactive.&#8217;</p>
<p>A <a title="io.BlockingIOError" class="reference internal" href="#io.BlockingIOError"><tt class="xref docutils literal"><span class="pre">BlockingIOError</span></tt></a> is raised if the underlying raw stream has no
data at the moment.</p>
</dd></dl>

<dl class="method">
<dt id="io.BufferedIOBase.write">
<tt class="descname">write</tt><big>(</big><em>b</em><big>)</big><a class="headerlink" href="#io.BufferedIOBase.write" title="Permalink to this definition">¶</a></dt>
<dd><p>Write the given bytes or bytearray object, <em>b</em>, to the underlying raw
stream and return the number of bytes written (never less than <tt class="docutils literal"><span class="pre">len(b)</span></tt>,
since if the write fails an <a title="exceptions.IOError" class="reference external" href="exceptions.html#exceptions.IOError"><tt class="xref docutils literal"><span class="pre">IOError</span></tt></a> will be raised).</p>
<p>A <a title="io.BlockingIOError" class="reference internal" href="#io.BlockingIOError"><tt class="xref docutils literal"><span class="pre">BlockingIOError</span></tt></a> is raised if the buffer is full, and the
underlying raw stream cannot accept more data at the moment.</p>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="io.BytesIO">
<em class="property">
class </em><tt class="descclassname">io.</tt><tt class="descname">BytesIO</tt><big>(</big><span class="optional">[</span><em>initial_bytes</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#io.BytesIO" title="Permalink to this definition">¶</a></dt>
<dd><p>A stream implementation using an in-memory bytes buffer.  It inherits
<a title="io.BufferedIOBase" class="reference internal" href="#io.BufferedIOBase"><tt class="xref docutils literal"><span class="pre">BufferedIOBase</span></tt></a>.</p>
<p>The argument <em>initial_bytes</em> is an optional initial bytearray.</p>
<p><a title="io.BytesIO" class="reference internal" href="#io.BytesIO"><tt class="xref docutils literal"><span class="pre">BytesIO</span></tt></a> provides or overrides these methods in addition to those
from <a title="io.BufferedIOBase" class="reference internal" href="#io.BufferedIOBase"><tt class="xref docutils literal"><span class="pre">BufferedIOBase</span></tt></a> and <a title="io.IOBase" class="reference internal" href="#io.IOBase"><tt class="xref docutils literal"><span class="pre">IOBase</span></tt></a>:</p>
<dl class="method">
<dt id="io.BytesIO.getvalue">
<tt class="descname">getvalue</tt><big>(</big><big>)</big><a class="headerlink" href="#io.BytesIO.getvalue" title="Permalink to this definition">¶</a></dt>
<dd>Return <tt class="docutils literal"><span class="pre">bytes</span></tt> containing the entire contents of the buffer.</dd></dl>

<dl class="method">
<dt id="io.BytesIO.read1">
<tt class="descname">read1</tt><big>(</big><big>)</big><a class="headerlink" href="#io.BytesIO.read1" title="Permalink to this definition">¶</a></dt>
<dd>In <a title="io.BytesIO" class="reference internal" href="#io.BytesIO"><tt class="xref docutils literal"><span class="pre">BytesIO</span></tt></a>, this is the same as <tt class="xref docutils literal"><span class="pre">read()</span></tt>.</dd></dl>

<dl class="method">
<dt id="io.BytesIO.truncate">
<tt class="descname">truncate</tt><big>(</big><span class="optional">[</span><em>size</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#io.BytesIO.truncate" title="Permalink to this definition">¶</a></dt>
<dd>Truncate the buffer to at most <em>size</em> bytes.  <em>size</em> defaults to the
current stream position, as returned by <tt class="xref docutils literal"><span class="pre">tell()</span></tt>.</dd></dl>

</dd></dl>

<dl class="class">
<dt id="io.BufferedReader">
<em class="property">
class </em><tt class="descclassname">io.</tt><tt class="descname">BufferedReader</tt><big>(</big><em>raw</em><span class="optional">[</span>, <em>buffer_size</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#io.BufferedReader" title="Permalink to this definition">¶</a></dt>
<dd><p>A buffer for a readable, sequential <a title="io.RawIOBase" class="reference internal" href="#io.RawIOBase"><tt class="xref docutils literal"><span class="pre">RawIOBase</span></tt></a> object.  It inherits
<a title="io.BufferedIOBase" class="reference internal" href="#io.BufferedIOBase"><tt class="xref docutils literal"><span class="pre">BufferedIOBase</span></tt></a>.</p>
<p>The constructor creates a <a title="io.BufferedReader" class="reference internal" href="#io.BufferedReader"><tt class="xref docutils literal"><span class="pre">BufferedReader</span></tt></a> for the given readable
<em>raw</em> stream and <em>buffer_size</em>.  If <em>buffer_size</em> is omitted,
<a title="io.DEFAULT_BUFFER_SIZE" class="reference internal" href="#io.DEFAULT_BUFFER_SIZE"><tt class="xref docutils literal"><span class="pre">DEFAULT_BUFFER_SIZE</span></tt></a> is used.</p>
<p><a title="io.BufferedReader" class="reference internal" href="#io.BufferedReader"><tt class="xref docutils literal"><span class="pre">BufferedReader</span></tt></a> provides or overrides these methods in addition to
those from <a title="io.BufferedIOBase" class="reference internal" href="#io.BufferedIOBase"><tt class="xref docutils literal"><span class="pre">BufferedIOBase</span></tt></a> and <a title="io.IOBase" class="reference internal" href="#io.IOBase"><tt class="xref docutils literal"><span class="pre">IOBase</span></tt></a>:</p>
<dl class="method">
<dt id="io.BufferedReader.peek">
<tt class="descname">peek</tt><big>(</big><span class="optional">[</span><em>n</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#io.BufferedReader.peek" title="Permalink to this definition">¶</a></dt>
<dd>Return 1 (or <em>n</em> if specified) bytes from a buffer without advancing the
position.  Only a single read on the raw stream is done to satisfy the
call. The number of bytes returned may be less than requested since at
most all the buffer&#8217;s bytes from the current position to the end are
returned.</dd></dl>

<dl class="method">
<dt id="io.BufferedReader.read">
<tt class="descname">read</tt><big>(</big><span class="optional">[</span><em>n</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#io.BufferedReader.read" title="Permalink to this definition">¶</a></dt>
<dd>Read and return <em>n</em> bytes, or if <em>n</em> is not given or negative, until EOF
or if the read call would block in non-blocking mode.</dd></dl>

<dl class="method">
<dt id="io.BufferedReader.read1">
<tt class="descname">read1</tt><big>(</big><em>n</em><big>)</big><a class="headerlink" href="#io.BufferedReader.read1" title="Permalink to this definition">¶</a></dt>
<dd>Read and return up to <em>n</em> bytes with only one call on the raw stream.  If
at least one byte is buffered, only buffered bytes are returned.
Otherwise, one raw stream read call is made.</dd></dl>

</dd></dl>

<dl class="class">
<dt id="io.BufferedWriter">
<em class="property">
class </em><tt class="descclassname">io.</tt><tt class="descname">BufferedWriter</tt><big>(</big><em>raw</em><span class="optional">[</span>, <em>buffer_size</em><span class="optional">[</span>, <em>max_buffer_size</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#io.BufferedWriter" title="Permalink to this definition">¶</a></dt>
<dd><p>A buffer for a writeable sequential RawIO object.  It inherits
<a title="io.BufferedIOBase" class="reference internal" href="#io.BufferedIOBase"><tt class="xref docutils literal"><span class="pre">BufferedIOBase</span></tt></a>.</p>
<p>The constructor creates a <a title="io.BufferedWriter" class="reference internal" href="#io.BufferedWriter"><tt class="xref docutils literal"><span class="pre">BufferedWriter</span></tt></a> for the given writeable
<em>raw</em> stream.  If the <em>buffer_size</em> is not given, it defaults to
<tt class="xref docutils literal"><span class="pre">DEAFULT_BUFFER_SIZE</span></tt>.  If <em>max_buffer_size</em> is omitted, it defaults to
twice the buffer size.</p>
<p><a title="io.BufferedWriter" class="reference internal" href="#io.BufferedWriter"><tt class="xref docutils literal"><span class="pre">BufferedWriter</span></tt></a> provides or overrides these methods in addition to
those from <a title="io.BufferedIOBase" class="reference internal" href="#io.BufferedIOBase"><tt class="xref docutils literal"><span class="pre">BufferedIOBase</span></tt></a> and <a title="io.IOBase" class="reference internal" href="#io.IOBase"><tt class="xref docutils literal"><span class="pre">IOBase</span></tt></a>:</p>
<dl class="method">
<dt id="io.BufferedWriter.flush">
<tt class="descname">flush</tt><big>(</big><big>)</big><a class="headerlink" href="#io.BufferedWriter.flush" title="Permalink to this definition">¶</a></dt>
<dd>Force bytes held in the buffer into the raw stream.  A
<a title="io.BlockingIOError" class="reference internal" href="#io.BlockingIOError"><tt class="xref docutils literal"><span class="pre">BlockingIOError</span></tt></a> should be raised if the raw stream blocks.</dd></dl>

<dl class="method">
<dt id="io.BufferedWriter.write">
<tt class="descname">write</tt><big>(</big><em>b</em><big>)</big><a class="headerlink" href="#io.BufferedWriter.write" title="Permalink to this definition">¶</a></dt>
<dd>Write the bytes or bytearray object, <em>b</em>, onto the raw stream and return
the number of bytes written.  A <a title="io.BlockingIOError" class="reference internal" href="#io.BlockingIOError"><tt class="xref docutils literal"><span class="pre">BlockingIOError</span></tt></a> is raised when the
raw stream blocks.</dd></dl>

</dd></dl>

<dl class="class">
<dt id="io.BufferedRWPair">
<em class="property">
class </em><tt class="descclassname">io.</tt><tt class="descname">BufferedRWPair</tt><big>(</big><em>reader</em>, <em>writer</em><span class="optional">[</span>, <em>buffer_size</em><span class="optional">[</span>, <em>max_buffer_size</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#io.BufferedRWPair" title="Permalink to this definition">¶</a></dt>
<dd><p>A combined buffered writer and reader object for a raw stream that can be
written to and read from.  It has and supports both <tt class="xref docutils literal"><span class="pre">read()</span></tt>, <tt class="xref docutils literal"><span class="pre">write()</span></tt>,
and their variants.  This is useful for sockets and two-way pipes.
It inherits <a title="io.BufferedIOBase" class="reference internal" href="#io.BufferedIOBase"><tt class="xref docutils literal"><span class="pre">BufferedIOBase</span></tt></a>.</p>
<p><em>reader</em> and <em>writer</em> are <a title="io.RawIOBase" class="reference internal" href="#io.RawIOBase"><tt class="xref docutils literal"><span class="pre">RawIOBase</span></tt></a> objects that are readable and
writeable respectively.  If the <em>buffer_size</em> is omitted it defaults to
<a title="io.DEFAULT_BUFFER_SIZE" class="reference internal" href="#io.DEFAULT_BUFFER_SIZE"><tt class="xref docutils literal"><span class="pre">DEFAULT_BUFFER_SIZE</span></tt></a>.  The <em>max_buffer_size</em> (for the buffered writer)
defaults to twice the buffer size.</p>
<p><a title="io.BufferedRWPair" class="reference internal" href="#io.BufferedRWPair"><tt class="xref docutils literal"><span class="pre">BufferedRWPair</span></tt></a> implements all of <a title="io.BufferedIOBase" class="reference internal" href="#io.BufferedIOBase"><tt class="xref docutils literal"><span class="pre">BufferedIOBase</span></tt></a>&#8216;s methods.</p>
</dd></dl>

<dl class="class">
<dt id="io.BufferedRandom">
<em class="property">
class </em><tt class="descclassname">io.</tt><tt class="descname">BufferedRandom</tt><big>(</big><em>raw</em><span class="optional">[</span>, <em>buffer_size</em><span class="optional">[</span>, <em>max_buffer_size</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#io.BufferedRandom" title="Permalink to this definition">¶</a></dt>
<dd><p>A buffered interface to random access streams.  It inherits
<a title="io.BufferedReader" class="reference internal" href="#io.BufferedReader"><tt class="xref docutils literal"><span class="pre">BufferedReader</span></tt></a> and <a title="io.BufferedWriter" class="reference internal" href="#io.BufferedWriter"><tt class="xref docutils literal"><span class="pre">BufferedWriter</span></tt></a>.</p>
<p>The constructor creates a reader and writer for a seekable raw stream, given
in the first argument.  If the <em>buffer_size</em> is omitted it defaults to
<a title="io.DEFAULT_BUFFER_SIZE" class="reference internal" href="#io.DEFAULT_BUFFER_SIZE"><tt class="xref docutils literal"><span class="pre">DEFAULT_BUFFER_SIZE</span></tt></a>.  The <em>max_buffer_size</em> (for the buffered writer)
defaults to twice the buffer size.</p>
<p><a title="io.BufferedRandom" class="reference internal" href="#io.BufferedRandom"><tt class="xref docutils literal"><span class="pre">BufferedRandom</span></tt></a> is capable of anything <a title="io.BufferedReader" class="reference internal" href="#io.BufferedReader"><tt class="xref docutils literal"><span class="pre">BufferedReader</span></tt></a> or
<a title="io.BufferedWriter" class="reference internal" href="#io.BufferedWriter"><tt class="xref docutils literal"><span class="pre">BufferedWriter</span></tt></a> can do.</p>
</dd></dl>

</div>
<div class="section" id="text-i-o">
<h2>16.2.5. Text I/O<a class="headerlink" href="#text-i-o" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="io.TextIOBase">
<em class="property">
class </em><tt class="descclassname">io.</tt><tt class="descname">TextIOBase</tt><a class="headerlink" href="#io.TextIOBase" title="Permalink to this definition">¶</a></dt>
<dd><p>Base class for text streams.  This class provides a character and line based
interface to stream I/O.  There is no <tt class="xref docutils literal"><span class="pre">readinto()</span></tt> method because
Python&#8217;s character strings are immutable.  It inherits <a title="io.IOBase" class="reference internal" href="#io.IOBase"><tt class="xref docutils literal"><span class="pre">IOBase</span></tt></a>.
There is no public constructor.</p>
<p><a title="io.TextIOBase" class="reference internal" href="#io.TextIOBase"><tt class="xref docutils literal"><span class="pre">TextIOBase</span></tt></a> provides or overrides these data attributes and
methods in addition to those from <a title="io.IOBase" class="reference internal" href="#io.IOBase"><tt class="xref docutils literal"><span class="pre">IOBase</span></tt></a>:</p>
<dl class="attribute">
<dt id="io.TextIOBase.encoding">
<tt class="descname">encoding</tt><a class="headerlink" href="#io.TextIOBase.encoding" title="Permalink to this definition">¶</a></dt>
<dd>The name of the encoding used to decode the stream&#8217;s bytes into
strings, and to encode strings into bytes.</dd></dl>

<dl class="attribute">
<dt id="io.TextIOBase.newlines">
<tt class="descname">newlines</tt><a class="headerlink" href="#io.TextIOBase.newlines" title="Permalink to this definition">¶</a></dt>
<dd>A string, a tuple of strings, or <tt class="xref docutils literal"><span class="pre">None</span></tt>, indicating the newlines
translated so far.</dd></dl>

<dl class="method">
<dt id="io.TextIOBase.read">
<tt class="descname">read</tt><big>(</big><em>n</em><big>)</big><a class="headerlink" href="#io.TextIOBase.read" title="Permalink to this definition">¶</a></dt>
<dd>Read and return at most <em>n</em> characters from the stream as a single
<a title="str" class="reference external" href="functions.html#str"><tt class="xref docutils literal"><span class="pre">str</span></tt></a>.  If <em>n</em> is negative or <tt class="xref docutils literal"><span class="pre">None</span></tt>, reads to EOF.</dd></dl>

<dl class="method">
<dt id="io.TextIOBase.readline">
<tt class="descname">readline</tt><big>(</big><big>)</big><a class="headerlink" href="#io.TextIOBase.readline" title="Permalink to this definition">¶</a></dt>
<dd>Read until newline or EOF and return a single <tt class="docutils literal"><span class="pre">str</span></tt>.  If the stream is
already at EOF, an empty string is returned.</dd></dl>

<dl class="method">
<dt id="io.TextIOBase.write">
<tt class="descname">write</tt><big>(</big><em>s</em><big>)</big><a class="headerlink" href="#io.TextIOBase.write" title="Permalink to this definition">¶</a></dt>
<dd>Write the string <em>s</em> to the stream and return the number of characters
written.</dd></dl>

</dd></dl>

<dl class="class">
<dt id="io.TextIOWrapper">
<em class="property">
class </em><tt class="descclassname">io.</tt><tt class="descname">TextIOWrapper</tt><big>(</big><em>buffer</em><span class="optional">[</span>, <em>encoding</em><span class="optional">[</span>, <em>errors</em><span class="optional">[</span>, <em>newline</em><span class="optional">[</span>, <em>line_buffering</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#io.TextIOWrapper" title="Permalink to this definition">¶</a></dt>
<dd><p>A buffered text stream over a <a title="io.BufferedIOBase" class="reference internal" href="#io.BufferedIOBase"><tt class="xref docutils literal"><span class="pre">BufferedIOBase</span></tt></a> raw stream, <em>buffer</em>.
It inherits <a title="io.TextIOBase" class="reference internal" href="#io.TextIOBase"><tt class="xref docutils literal"><span class="pre">TextIOBase</span></tt></a>.</p>
<p><em>encoding</em> gives the name of the encoding that the stream will be decoded or
encoded with.  It defaults to <a title="locale.getpreferredencoding" class="reference external" href="locale.html#locale.getpreferredencoding"><tt class="xref docutils literal"><span class="pre">locale.getpreferredencoding()</span></tt></a>.</p>
<p><em>errors</em> is an optional string that specifies how encoding and decoding
errors are to be handled.  Pass <tt class="docutils literal"><span class="pre">'strict'</span></tt> to raise a <a title="exceptions.ValueError" class="reference external" href="exceptions.html#exceptions.ValueError"><tt class="xref docutils literal"><span class="pre">ValueError</span></tt></a>
exception if there is an encoding error (the default of <tt class="xref docutils literal"><span class="pre">None</span></tt> has the same
effect), or pass <tt class="docutils literal"><span class="pre">'ignore'</span></tt> to ignore errors.  (Note that ignoring encoding
errors can lead to data loss.)  <tt class="docutils literal"><span class="pre">'replace'</span></tt> causes a replacement marker
(such as <tt class="docutils literal"><span class="pre">'?'</span></tt>) to be inserted where there is malformed data.  When
writing, <tt class="docutils literal"><span class="pre">'xmlcharrefreplace'</span></tt> (replace with the appropriate XML character
reference) or <tt class="docutils literal"><span class="pre">'backslashreplace'</span></tt> (replace with backslashed escape
sequences) can be used.  Any other error handling name that has been
registered with <a title="codecs.register_error" class="reference external" href="codecs.html#codecs.register_error"><tt class="xref docutils literal"><span class="pre">codecs.register_error()</span></tt></a> is also valid.</p>
<p><em>newline</em> can be <tt class="xref docutils literal"><span class="pre">None</span></tt>, <tt class="docutils literal"><span class="pre">''</span></tt>, <tt class="docutils literal"><span class="pre">'\n'</span></tt>, <tt class="docutils literal"><span class="pre">'\r'</span></tt>, or <tt class="docutils literal"><span class="pre">'\r\n'</span></tt>.  It
controls the handling of line endings.  If it is <tt class="xref docutils literal"><span class="pre">None</span></tt>, universal newlines
is enabled.  With this enabled, on input, the lines endings <tt class="docutils literal"><span class="pre">'\n'</span></tt>,
<tt class="docutils literal"><span class="pre">'\r'</span></tt>, or <tt class="docutils literal"><span class="pre">'\r\n'</span></tt> are translated to <tt class="docutils literal"><span class="pre">'\n'</span></tt> before being returned to
the caller.  Conversely, on output, <tt class="docutils literal"><span class="pre">'\n'</span></tt> is translated to the system
default line seperator, <a title="os.linesep" class="reference external" href="os.html#os.linesep"><tt class="xref docutils literal"><span class="pre">os.linesep</span></tt></a>.  If <em>newline</em> is any other of its
legal values, that newline becomes the newline when the file is read and it
is returned untranslated.  On output, <tt class="docutils literal"><span class="pre">'\n'</span></tt> is converted to the <em>newline</em>.</p>
<p>If <em>line_buffering</em> is <tt class="xref docutils literal"><span class="pre">True</span></tt>, <tt class="xref docutils literal"><span class="pre">flush()</span></tt> is implied when a call to
write contains a newline character.</p>
<p><a title="io.TextIOWrapper" class="reference internal" href="#io.TextIOWrapper"><tt class="xref docutils literal"><span class="pre">TextIOWrapper</span></tt></a> provides these data attributes in addition to those of
<a title="io.TextIOBase" class="reference internal" href="#io.TextIOBase"><tt class="xref docutils literal"><span class="pre">TextIOBase</span></tt></a> and its parents:</p>
<dl class="attribute">
<dt id="io.TextIOWrapper.errors">
<tt class="descname">errors</tt><a class="headerlink" href="#io.TextIOWrapper.errors" title="Permalink to this definition">¶</a></dt>
<dd>The encoding and decoding error setting.</dd></dl>

<dl class="attribute">
<dt id="io.TextIOWrapper.line_buffering">
<tt class="descname">line_buffering</tt><a class="headerlink" href="#io.TextIOWrapper.line_buffering" title="Permalink to this definition">¶</a></dt>
<dd>Whether line buffering is enabled.</dd></dl>

</dd></dl>

<dl class="class">
<dt id="io.StringIO">
<em class="property">
class </em><tt class="descclassname">io.</tt><tt class="descname">StringIO</tt><big>(</big><span class="optional">[</span><em>initial_value</em><span class="optional">[</span>, <em>encoding</em><span class="optional">[</span>, <em>errors</em><span class="optional">[</span>, <em>newline</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#io.StringIO" title="Permalink to this definition">¶</a></dt>
<dd><p>An in-memory stream for text.  It in inherits <a title="io.TextIOWrapper" class="reference internal" href="#io.TextIOWrapper"><tt class="xref docutils literal"><span class="pre">TextIOWrapper</span></tt></a>.</p>
<p>Create a new StringIO stream with an inital value, encoding, error handling,
and newline setting.  See <a title="io.TextIOWrapper" class="reference internal" href="#io.TextIOWrapper"><tt class="xref docutils literal"><span class="pre">TextIOWrapper</span></tt></a>&#8216;s constructor for more
information.</p>
<p><a title="io.StringIO" class="reference internal" href="#io.StringIO"><tt class="xref docutils literal"><span class="pre">StringIO</span></tt></a> provides this method in addition to those from
<a title="io.TextIOWrapper" class="reference internal" href="#io.TextIOWrapper"><tt class="xref docutils literal"><span class="pre">TextIOWrapper</span></tt></a> and its parents:</p>
<dl class="method">
<dt id="io.StringIO.getvalue">
<tt class="descname">getvalue</tt><big>(</big><big>)</big><a class="headerlink" href="#io.StringIO.getvalue" title="Permalink to this definition">¶</a></dt>
<dd>Return a <tt class="docutils literal"><span class="pre">str</span></tt> containing the entire contents of the buffer.</dd></dl>

</dd></dl>

<dl class="class">
<dt id="io.IncrementalNewlineDecoder">
<em class="property">
class </em><tt class="descclassname">io.</tt><tt class="descname">IncrementalNewlineDecoder</tt><a class="headerlink" href="#io.IncrementalNewlineDecoder" title="Permalink to this definition">¶</a></dt>
<dd>A helper codec that decodes newlines for universal newlines mode.  It
inherits <a title="codecs.IncrementalDecoder" class="reference external" href="codecs.html#codecs.IncrementalDecoder"><tt class="xref docutils literal"><span class="pre">codecs.IncrementalDecoder</span></tt></a>.</dd></dl>

</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../contents.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="">16.2. <tt class="docutils literal"><span class="pre">io</span></tt> &#8212; Core tools for working with streams</a><ul>
<li><a class="reference external" href="#module-interface">16.2.1. Module Interface</a></li>
<li><a class="reference external" href="#i-o-base-classes">16.2.2. I/O Base Classes</a></li>
<li><a class="reference external" href="#raw-file-i-o">16.2.3. Raw File I/O</a></li>
<li><a class="reference external" href="#buffered-streams">16.2.4. Buffered Streams</a></li>
<li><a class="reference external" href="#text-i-o">16.2.5. Text I/O</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="os.html"
                                  title="previous chapter">16.1. <tt class="docutils literal docutils literal"><span class="pre">os</span></tt> &#8212; Miscellaneous operating system interfaces</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="time.html"
                                  title="next chapter">16.3. <tt class="docutils literal"><span class="pre">time</span></tt> &#8212; Time access and conversions</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/library/io.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="time.html" title="16.3. time — Time access and conversions"
             >next</a> |</li>
        <li class="right" >
          <a href="os.html" title="16.1. os — Miscellaneous operating system interfaces"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="allos.html" >16. Generic Operating System Services</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; <a href="../copyright.html">Copyright</a> 1990-2009, Python Software Foundation.
      Last updated on Apr 15, 2009.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.1.
    </div>
  </body>
</html>